package com.lyh.system.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lyh.common.core.domain.entity.SysUser;
import com.lyh.common.enums.PayStatusEnum;
import com.lyh.common.enums.RechargeTypeEnum;
import com.lyh.common.utils.SecurityUtils;
import com.lyh.system.biz.IPayBiz;
import com.lyh.system.domain.Goods;
import com.lyh.system.domain.Order;
import com.lyh.system.domain.RechargeRecord;
import com.lyh.system.domain.UserPayRecordDO;
import com.lyh.system.domain.req.ZfbCashAdvanceReq;
import com.lyh.system.domain.req.ZfbPayCommonReq;
import com.lyh.system.domain.resp.WxPayInfoResp;
import com.lyh.system.mapper.GoodsMapper;
import com.lyh.system.mapper.SysUserMapper;
import com.lyh.system.mapper.UserPayRecordMapper;
import com.lyh.system.service.IPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class PayBizImpl implements IPayBiz {

    @Autowired
    private UserPayRecordMapper userPayRecordMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     *  保存支付下单的信息
     *
     * @param order
     */
    @Override
    public int savaPayRecordPrepare(Order order,String json) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        UserPayRecordDO recordDO = new UserPayRecordDO();
        recordDO.setSysOrderNum(order.getOrderNum());
        recordDO.setUserId(sysUser.getUserId());
        recordDO.setPayMoney(order.getActualPayment());
        recordDO.setOrderId(order.getId());
        recordDO.setReqBody(json);
        recordDO.setStatus(PayStatusEnum.WAIT.getCode());
        return userPayRecordMapper.insert(recordDO);
    }

    @Override
    public int updatePayRecordInfo(WxPayInfoResp payBackInfo,String respJson) {
        UserPayRecordDO record = userPayRecordMapper.selectOne(new LambdaQueryWrapper<UserPayRecordDO>()
                .eq(UserPayRecordDO::getSysOrderNum, payBackInfo.getOut_trade_no()));
        if(record != null){
            record.setRespBody(respJson);
            record.setStatus(payBackInfo.getTrade_state());
            record.setPayOrderNum(payBackInfo.getTransactionId());
            return userPayRecordMapper.updateById(record);
        }
        return 0;
    }

    @Override
    public int refundOrder(Order order, String json) {


        return 0;
    }

    @Autowired
    private IPayService alipayService;
    @Override
    public String zfbRechargePay(RechargeRecord order) {
        ZfbPayCommonReq commonReq = new ZfbPayCommonReq();
        commonReq.setBusinessId(order.getId());
        commonReq.setGoodsName(RechargeTypeEnum.getEnum(order.getRechargeType()).name());
        commonReq.setTotalAmount(order.getRechargeAmount().toString());
        commonReq.setOrderNum(order.getOrderId());
        commonReq.setOrderSource("recharge");
        String s = alipayService.zfbPayCommom(commonReq);
        return s;
    }

    @Override
    public String zfbPay(Order order) {

        ZfbPayCommonReq commonReq = new ZfbPayCommonReq();
        commonReq.setBusinessId(order.getId());
        Goods goods = goodsMapper.selectById(order.getGoodsId());
        commonReq.setGoodsName(goods.getName());
        commonReq.setTotalAmount(order.getActualPayment().toString());
        commonReq.setOrderNum(order.getOrderNum());
        commonReq.setOrderSource("order");
        String s = alipayService.zfbPayCommom(commonReq);
        return s;
    }

    @Override
    public String zfbRefund(Order order) {
        return alipayService.zfbRefund(order);

    }

    @Override
    public boolean zfbAdvance(ZfbCashAdvanceReq req) {
        //查找实名
        return alipayService.zfbCashAdvance(req);
    }
}
